﻿using System.Windows;
using System.Windows.Controls;

namespace Dsl.Ped.Framework.WPFControls.NumericTextBox 
{
    /// <summary>
    /// <see cref="TextBox"/> control that supress edit of non-numberic content
    /// </summary>
    public class NumericTextBoxUserControl : TextBox {
        public static readonly DependencyProperty MaskProperty =
            DependencyProperty.RegisterAttached(
                "Mask",
                typeof(MaskType),
                typeof(NumericTextBoxUserControl),
                new FrameworkPropertyMetadata(new PropertyChangedCallback(onMaskTypeChanged)));

        public MaskType Mask {
            get {
                return (MaskType)this.GetValue(MaskProperty);
            }
            set {
                this.SetValue(MaskProperty, value);
            }
        }

        public static readonly DependencyProperty MinimumValueProperty =
            DependencyProperty.RegisterAttached(
                "MinimumValue",
                typeof(double),
                typeof(NumericTextBoxUserControl),
                new FrameworkPropertyMetadata(double.NaN, new PropertyChangedCallback(onMinimumValueChanged)));

        public double MinimumValue {
            get {
                return (double)this.GetValue(MinimumValueProperty);
            }
            set {
                this.SetValue(MinimumValueProperty, value);
            }
        }

        public static readonly DependencyProperty MaximumValueProperty =
            DependencyProperty.RegisterAttached(
                "MaximumValue",
                typeof(double),
                typeof(NumericTextBoxUserControl),
                new FrameworkPropertyMetadata(double.NaN, new PropertyChangedCallback(onMaximumValueChanged)));

        public double MaximumValue {
            get {
                return (double)this.GetValue(MaximumValueProperty);
            }
            set {
                this.SetValue(MaximumValueProperty, value);
            }
        }

        public NumericTextBoxUserControl() {
            TextBoxMaskBehavior.SetMask(this, MaskType.Any);
        }

        #region Methods

        private static void onMaskTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) {
            NumericTextBoxUserControl numericTextBoxControl = (NumericTextBoxUserControl)sender;
            MaskType maskType = (MaskType)e.NewValue;

            TextBoxMaskBehavior.SetMask(numericTextBoxControl, maskType);
        }

        private static void onMinimumValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) {
            NumericTextBoxUserControl numericTextBoxControl = (NumericTextBoxUserControl)sender;
            double minimumValue = (double)e.NewValue;

            TextBoxMaskBehavior.SetMinimumValue(numericTextBoxControl, minimumValue);
        }

        private static void onMaximumValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) {
            NumericTextBoxUserControl numericTextBoxControl = (NumericTextBoxUserControl)sender;
            double maximumValue = (double)e.NewValue;

            TextBoxMaskBehavior.SetMaximumValue(numericTextBoxControl, maximumValue);
        }

        #endregion
    }
}
